﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ZLDC.Common.Estate
{
    public class PageExport
    {
        public static string ExportTable(DataSet ds)
        {
            StringBuilder sb = new StringBuilder();
            int count = 0;
            foreach (DataTable tb in ds.Tables)
            {
                sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
                sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
                sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
                foreach (DataColumn column in tb.Columns)
                {
                    if (column.ColumnName.Equals("部门编号") || column.ColumnName.Equals("人员编号"))
                        sb.AppendLine("");
                    else
                        sb.AppendLine("<td>" + column.ColumnName + "</td>");
                }
                sb.AppendLine("</tr>");

                foreach (DataRow row in tb.Rows)
                {
                    sb.Append("<tr>");
                    foreach (DataColumn column in tb.Columns)
                    {
                        if (column.ColumnName.Equals("部门编号") || column.ColumnName.Equals("人员编号"))
                            sb.Append("");
                        else
                            sb.Append("<td>" + row[column].ToString() + "</td>");
                    }
                    sb.AppendLine("</tr>");
                    count++;
                }
                sb.AppendLine("</table>");
            }

            return sb.ToString();
        }



        public static void ExportDsToXls(System.Web.UI.Page page, string sql)
        {
            ExportDsToXls(page, "FileName", sql);
        }
        public static void ExportDsToXls(System.Web.UI.Page page, string fileName, string sql)
        {
            using (_Database db = new _Database("estate"))
            {
                DataSet ds = db.Query(sql);
                if (ds != null) ExportDsToXls(page, fileName, ds);
            }
        }
        public static void ExportDsToXls(System.Web.UI.Page page, DataSet ds)
        {
            ExportDsToXls(page, "FileName", ds);
        }
        public static void ExportDsToXls(System.Web.UI.Page page, string fileName, DataSet ds)
        {
            page.Response.Clear();
            page.Response.Buffer = true;
            page.Response.Charset = "GB2312";
            page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); page.Response.ContentType = "application/ms-excel"; page.EnableViewState = false;
            page.Response.Write(ExportTable(ds));
            page.Response.End();
        }


    }
}
